package com.study.kafkademo.kafka.defalut;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class DefaultKafkaConsumer {

    @KafkaListener(topics = DefaultKafkaProducer.TOPIC_TEST, groupId = DefaultKafkaProducer.TOPIC_GROUP1)
    public void topic_test(ConsumerRecord record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        final Optional<ConsumerRecord> message = Optional.ofNullable(record);
        if (message.isPresent()) {
            final ConsumerRecord consumerRecord = message.get();
            log.info("topic_test 消费了： topic：" + topic + ";;; message :" + consumerRecord);
            ack.acknowledge();
        }
    }

    @KafkaListener(topics = DefaultKafkaProducer.TOPIC_TEST, groupId = DefaultKafkaProducer.TOPIC_GROUP2)
    public void topic_test1(ConsumerRecord record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        final Optional<ConsumerRecord> message = Optional.ofNullable(record);
        if (message.isPresent()) {
            final ConsumerRecord consumerRecord = message.get();
            log.info("topic_test1 消费了： topic：" + topic + ";;; message :" + consumerRecord);
            ack.acknowledge();
        }
    }
}
